import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SFireworkOpen } from "../../../Message/Public/SFireworkOpen";
import { SFireworkEffect } from "../../../Message/Public/SFireworkEffect";
import { ItemData } from "../resource/info/ItemData";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { FWManager } from "../../../extend/flash/fireworks/FWManager";
import { FireWorkType } from "../../../extend/flash/fireworks/FireWorkType";
import { FirePlayType } from "../../../extend/flash/fireworks/FirePlayType";
import { FlowerManager } from "../../../extend/flash/flower/FlowerManager";
import { FlowerType } from "../../../extend/flash/flower/FlowerType";
import { SFireworkEventRecord } from "../../../Message/Game/SFireworkEventRecord";
import { SFireworkBright } from "../../../Message/Public/SFireworkBright";
import { GameProxy } from "../mvc/GameProxy";
import { DataEvent } from "../events/DataEvent";
import { FireworkIconView } from "../view/firework/FireworkIconView";
import { FireworkEventRecordsWindow } from "../view/firework/FireworkEventRecordsWindow";
import { FireworkBrightView } from "../view/firework/FireworkBrightView";
type int = number;
//class FireworkController
    
    export  class FireworkController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.FireworkOpenCommand, this.onFireworkOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.FireWorkCloseCommand, this.onFireworkCloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.FireWorkEffectCommand, this.onFireworkEffectHandler);
            NetDispatcher.addCmdListener(ServerCommand.FireWorkEventRecordCommand, this.onFireworkEventRecordHandler);
            NetDispatcher.addCmdListener(ServerCommand.FireWorkBrightCommand, this.onFireworkBrightHandler);
            Dispatcher.addEventListener(EventName.FireworkIconClick, this.onFireworkIconClick);
            return;
        }

        /* internal  */onFireworkOpenHandler(arg1: SFireworkOpen): void
        {
            this.fireworkBrightView.show();
            this.fireIconView.show();
            return;
        }

        /* internal  */onFireworkCloseHandler(arg1: Object): void
        {
            this.fireworkBrightView.hide();
            this.fireIconView.hide();
            this.fireworkEventRecordsWindow.hide();
            this.dispose();
            return;
        }

        /* internal  */onFireworkEffectHandler(arg1: SFireworkEffect): void
        {
            var loc3=null;
            var loc1=new ItemData(arg1.itemCode);
            var loc2=GameMapUtil.getPixelPoint(arg1.x, arg1.y);
            if (ItemsUtil.isFireworkProp(loc1)) 
            {
                if (loc1.itemInfo.item.effectEx) 
                {
                    FWManager.instance.popup(1, 2, loc2, FireWorkType.MarryFire3, loc1.itemInfo.item.effectEx + ".swf", FirePlayType.TogetherPlay);
                }
                else 
                {
                    FWManager.instance.popup(0, 0, loc2);
                }
            }
            else if (ItemsUtil.isWeddingBubble(loc1)) 
            {
                FlowerManager.instance.addFlowerQueue(FlowerType.BALLON, 15000);
            }
            else if (ItemsUtil.isWeddingFirework(loc1) || ItemsUtil.isGuildPartyHot(loc1)) 
            {
                loc3 = loc1.itemInfo.item.effectEx + ".swf";
                if (loc1.itemInfo.item.level != 1) 
                {
                    FWManager.instance.popup(1, 2, loc2, FireWorkType.NormalFire, loc3, FirePlayType.OrderPlay, 300);
                }
                else 
                {
                    FWManager.instance.popup(1, 2, loc2, FireWorkType.MarryWeddingFire, loc3, FirePlayType.TogetherPlay);
                }
            }
            return;
        }

        /* internal  */onFireworkEventRecordHandler(arg1: SFireworkEventRecord): void
        {
            this.fireworkEventRecordsWindow.show();
            this.fireworkEventRecordsWindow.updateRecords(arg1);
            return;
        }

        /* internal  */onFireworkBrightHandler(arg1: SFireworkBright): void
        {
            var loc1=arg1.bright;
            if (loc1 <= this.maxBright) 
            {
                this.fireworkBrightView.updateBright(arg1.bright, this.maxBright);
            }
            else 
            {
                this.isOverBright = true;
                this.fireworkBrightView.updateBright(this.maxBright, this.maxBright);
            }
            if (this._fireworkEventRecordsWindow && !this.fireworkEventRecordsWindow.isHide) 
            {
                GameProxy.firework.getFireworkRecords();
            }
            return;
        }

        /* internal  */onFireworkIconClick(arg1: DataEvent): void
        {
            if (this.fireworkEventRecordsWindow.isHide) 
            {
                GameProxy.firework.getFireworkRecords();
            }
            else 
            {
                this.fireworkEventRecordsWindow.hide();
            }
            return;
        }

        /* internal  */get fireIconView(): FireworkIconView
        {
            if (!this._fireworkIconView) 
            {
                this._fireworkIconView = new FireworkIconView();
            }
            return this._fireworkIconView;
        }

        /* internal  */get fireworkEventRecordsWindow(): FireworkEventRecordsWindow
        {
            if (!this._fireworkEventRecordsWindow) 
            {
                this._fireworkEventRecordsWindow = new FireworkEventRecordsWindow();
            }
            return this._fireworkEventRecordsWindow;
        }

        public isHaveBrightView(): Boolean
        {
            return !(this._fireworkBrightView == null);
        }

        public get fireworkBrightView(): FireworkBrightView
        {
            if (!this._fireworkBrightView) 
            {
                this._fireworkBrightView = new FireworkBrightView();
            }
            return this._fireworkBrightView;
        }

        public dispose(): void
        {
            this._fireworkIconView = null;
            this._fireworkEventRecordsWindow = null;
            this._fireworkBrightView = null;
            return;
        }

        private /* const */maxBright: int=2013;

        private /* var */isOverBright: Boolean=false;

        private /* var */_fireworkIconView: FireworkIconView;

        private /* var */_fireworkEventRecordsWindow: FireworkEventRecordsWindow;

        private /* var */_fireworkBrightView: FireworkBrightView;
    }
